﻿
<!DOCTYPE html>

<html class="x-admin-sm">
<head>
    <meta name="viewport" content="width=device-width" />
    <meta charset="UTF-8">
    <title>设置角色权限</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
   <link href="~/X-admin/css/font.css" rel="stylesheet" />
    <script src="~/X-admin/lib/layui/layui.js"></script>
    <link href="~/X-admin/css/xadmin.css" rel="stylesheet" />
    <script src="~/X-admin/js/jquery.min.js"></script>
    <script src="~/X-admin/js/xadmin.js"></script>
    <script src="~/js/framework/sysframework.js"></script>
    <script src="~/js/layui/layui.js"></script>
    <link href="~/js/layui/css/layui.css" rel="stylesheet" />
</head>
<body>

    <div style="margin-left: 10px; padding: 5px; width: 95%;">

        <div style="float:left;width:100%">
            <div id="treeNav" class="demo-tree demo-tree-box" style="width: 190px; height: 350px; overflow: scroll;float:left;"></div>
            <div style="border:1px;width:77%;float:left;margin-left:10px;background-color:antiquewhite">
                <span style="margin-top:15px">
                    <input type="text" id="webName" name="webName" class="layui-input" readonly />
                </span>
                <div style="margin-left:15px">
                    <form class="layui-form layui-col-space5">
                        <div id="checkbox" style="margin-top:15px;font-weight:100">
                        </div>
                    </form>
                    </div>
                    <div style="width:90%;float:left;padding-left:8%;margin-top:20px">
                       <xblock>
                        <button class="layui-btn" onclick="SavePower()"><i class="layui-icon">&#xe61a;</i>保存</button>
                        <button class="layui-btn" onclick="cleanPower()"><i class="layui-icon">&#x1006;</i>清空权限</button>
                        <button class="layui-btn" onclick="closeWin()"><i class="layui-icon">&#x1006;</i>取消</button>
                       </xblock>
                    </div>
</div>

        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    var menuCode = "";
    var menuName = "";
    var gCode = "";
    $(function () {
        gCode = getUrlVariable("groupCode");
        autoHeight("treeNav");
        $.ajax({
            url: "../System/layUiTree",
            dataType: "json",
            type: "POST",
            success: function (rsl) {
                if (rsl.result) {
                    loadTree(rsl.data);
                } else {
                    layer.msg(rsl.message);
                }
            }
        });

    })
    /**
    加载树状菜单
    **/
    function loadTree(data) {
        var tree = layui.tree;
        var layer = layui.layer;
        var util = layui.util;
        //开启节点操作图标
        tree.render({
            elem: '#treeNav'
          , data: data
          , click: function (obj) {
              menuCode = obj.data["id"];
              menuName = obj.data["title"];
              $("input[ name='webName']").val("已选菜单："+menuName);
                showMenPower(menuCode);

          }
        });
    }


    var userCode = "";
    /**
    界面权限显示
    **/
    function showMenPower(menuCode) {
        $.ajax({
            url: "../System/GetWebPowerByCode",
            data: { menuCode: menuCode, groupCode: gCode },
            dataType: "json",
            type: "POST",
            success: function (data) {
                if (data.result) {
                    var wPowers = data.data["webPowers"];
                    var hPowers = data.data["hasPowers"];
                    PowersShow(wPowers, data.data);
                    console.log(hPowers);
                    SetCheckBoxValue("checkop", hPowers);
                } else {
                    layer.msg(data.Message);
                }
            }
        });
    }



    /**系统权限信息显示
    powers:当前页面所有权限
    havePower:用户拥有页面的操作权限
    **/
    function PowersShow(powers, havePower) {
        var str = "";
        var ps = "";
        for(var i=0;i<powers.length;i++){
            var title = powers[i].wpName + "(" + powers[i].pCode + ")";
            ps += "<input lay-skin='primary' type='checkbox'  value='" + powers[i].pCode + "'  title='" + title + "'  name='checkop' />";
        }
      
        if(ps!=""){
            $("#checkbox").empty();
            $("#checkbox").append(ps);
        }else{
            $("#checkbox").empty();
            $("#checkbox").append( "此页面没有配置任何权限");
        }
        layui.use('form', function () {
            var form = layui.form;
            form.render();
        });
        return;
   
        
    }
    /**
    权限保存
    **/
    function SavePower() {
        try {
            var vstr = "";
            $("input[type=checkbox][name=checkop]:checked").each(function () { //由于复选框一般选中的是多个,所以可以循环输出
                vstr += $(this).val() + ",";
            });
            vstr = vstr.slice(0, -1);
            $.ajax({
                url: "../System/SaveRolePower",
                data: { groupCode: gCode, menuCode: menuCode, powers: vstr },
                dataType: "json",
                type: "POST",
                success: function (data) {
                    if (data.result) {
                        layer.msg("权限配置成功");
                    } else {
                        layer.msg(data.Message);
                    }
                }
            });
        } catch (e) {
            layer.msg(e.message);
          //  window.location = "/Error/Error";
        }
    }

    function cleanPower() { 
        layer.confirm('确定清空此菜单权限？', function (index) {
            $.ajax({
                url: "../System/CleanWebPower",
                data: { groupCode: gCode, menuCode: menuCode},
                dataType: "json",
                type: "POST",
                success: function (data) {
                    if (data.result) {
                        layer.msg("权限配置成功");
                    } else {
                        layer.msg(data.Message);
                    }
                }
            });
            layer.close(index);
        });
    }

</script>
